const enterpriseVue = new Vue({
  el: '#app',
  data() {
    //校验身份证号
    var checkIDCard = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('身份证号不能为空'));
      }
      setTimeout(() => {
        if (!ID_Validity(value)) {
          callback(new Error('请输入正确的身份证号'));
        }
      }, 100);
    }
    return {
      pickerOptions: pickerOptions,
      rules: {
        Supplier_SerialNumber: [{ required: true, message: '请选择供应商', trigger: 'change' }],
        Purchase_Date: [{ required: true, message: '请输入采购日期', trigger: 'blur' }],
        Purchase_Amount: [{ required: true, message: '请输入订单金额', trigger: 'blur' }],
        Store_SerialNumber: [{ required: true, message: '请选择仓库', trigger: 'change' }],
        Quantity: [{ required: true, message: '请输入数量', trigger: 'blur' }],
        Purchase_UnitPrice: [{ required: true, message: '请输入成交单价', trigger: 'blur' }],
      },
      form: {
        Purchase_Order_SerialNumber: '',
        Supplier_SerialNumber: '',
        Purchase_Date: '',
        Purchase_Amount: 0,
        Paid_Amount: 0,
        Return_Amount: 0,
        Apply_Amount: 0,
        Confirm_Amount: 0,
        Enterprise_Info_SerialNumber: $.cookie("enterprise"),
        Row_Status: 5,
        Children: {
          purchase_order_detail: [{       //采购订单明细
            Purchase_Order_Detail_SerialNumber: "",
            Purchase_Order_SerialNumber: '',
            Brand_SerialNumber: '',
            Article_SerialNumber: '',
            Article_UnitPrice: '',
            Purchase_UnitPrice: '',
            Quantity: '',
            Store_SerialNumber: "",
            Depot_Type: 0,
            Receipt_Quantity: 0,
            Not_Receipt_Quantity: 0,
            Shippment_Quantity: 0,
            Inventory_Quantity: 0,
            Amount: 0,
            article: []
          }],
        }
      },
      Wares: [],
      supplier_info: [],
      brand: [],
      loading: false
    }
  },
  mounted() {
    this.getData()
    this.getWares()
  },
  methods: {
    getWares() {//获取所有可选仓库
      let this_ = this
      $.ajax({
        url: DomainName + '/getInquiryAction?token=' + $.cookie("token"),
        type: 'POST',
        data: {
          Data: 'warehouse_info',
        },
        success: function (res) {
          res.forEach(item => {
            this_.Wares.push({
              label: item.Warehouse_Name,
              value: item.Applicable_Type == '0' ? item.Warehouse_SerialNumber : item.Parent_Warehouse_SerialNumber
            })
          })
          console.log(this_.Wares)
        },
        dataType: "json"
      });
    },
    getData() {
      const _this = this
      $.ajax({
        url: `${DomainName}/getInquiryAction?token=${$.cookie('token')}`,
        type: 'POST',
        data: {
          Data: 'supplier_info'
        },
        success: function (res) {
          console.log(res)
          _this.supplier_info = res.filter(item=>item.Settled_Enterprise_Info_SerialNumber == '')
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试")
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
      $.ajax({
        url: `${DomainName}/GoodsFileInquiry`,
        type: 'POST',
        data: {
          Data: 'brand'
        },
        success: function (res) {
          _this.brand = res;
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试")
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    getArticle(e, id) {
      this.form.Children.purchase_order_detail[id].Article_SerialNumber = ''
      this.form.Children.purchase_order_detail[id].Article_UnitPrice = ''
      this.form.Children.purchase_order_detail[id].Purchase_UnitPrice = ''
      this.form.Children.purchase_order_detail[id].Quantity = ''
      this.form.Children.purchase_order_detail[id].Amount = ''
      this.count(id)
      const _this = this
      $.ajax({
        url: `${DomainName}/getInquiryIDAction?token=${$.cookie('token')}`,
        type: 'POST',
        data: {
          Data: 'article_info',
          Primary_Key_Name: 'Brand_SerialNumber',
          Primary_Key: e
        },
        success: function (res) {
          _this.form.Children.purchase_order_detail[id].article = res
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试")
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    count(id) {
      if (this.form.Children.purchase_order_detail[id].Purchase_UnitPrice != '' && this.form.Children.purchase_order_detail[id].Quantity != '') {
        this.form.Children.purchase_order_detail[id].Amount = this.accMul(this.form.Children.purchase_order_detail[id].Purchase_UnitPrice, this.form.Children.purchase_order_detail[id].Quantity)
        this.form.Purchase_Amount = 0
        this.form.Children.purchase_order_detail.forEach(item => {
          this.form.Purchase_Amount = this.accAdd(this.form.Purchase_Amount, item.Amount)
        })
      } else {
        this.form.Children.purchase_order_detail[id].Amount = ''
        this.form.Purchase_Amount = 0
        this.form.Children.purchase_order_detail.forEach(item => {
          this.form.Purchase_Amount = this.accAdd(this.form.Purchase_Amount, item.Amount)
        })
      }
    },
    accMul(arg1, arg2) {	//浮点型相乘
      let m = 0, s1 = arg1.toString(), s2 = arg2.toString();
      try { m += s1.split(".")[1].length } catch (e) { }
      try { m += s2.split(".")[1].length } catch (e) { }
      return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
    },
    accAdd(arg1, arg2) {	//浮点型相加
      var r1, r2, m;
      try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
      try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
      m = Math.pow(10, Math.max(r1, r2))
      return (arg1 * m + arg2 * m) / m
    },
    getUnitPrice(e, id) {
      let arr = this.form.Children.purchase_order_detail[id].article
      arr.forEach(item => {
        if (item.Article_SerialNumber == e) {
          this.form.Children.purchase_order_detail[id].Article_UnitPrice = item.Unit_Price
        }
      })

    },
    submitForm(formName) {
      let _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.loading = true
          $.ajax({
            url: DomainName + '/setPurchaseOrderAction?token=' + $.cookie("token"),
            type: 'POST',
            data: {
              Data: JSON.stringify({
                purchase_order: [_this.form]
              }),
              token: $.cookie("token")
            },
            success: function (res) {
              if (res.Status === 'success') {
                _this.$message.success("提交成功")
                window.location.href = './PurchaseOrder.html'
              } else {
                _this.$message.error("提交失败，请稍后再试")
              }
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试")
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }

      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    add_purchase_order_detail() {
      this.form.Children.purchase_order_detail.push({
        Purchase_Order_Detail_SerialNumber: "",
        Purchase_Order_SerialNumber: '',
        Brand_SerialNumber: '',
        Article_SerialNumber: '',
        Article_UnitPrice: '',
        Purchase_UnitPrice: '',
        Quantity: '',
        Amount: '',
        Store_SerialNumber: "",
        Depot_Type: 0,
        article: []
      })
    },
    remove_purchase_order_detail() {
      if (this.form.Children.purchase_order_detail.length > 1) {
        this.form.Children.purchase_order_detail.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    Pricethousands(num){//转千分位
      return thousands(num)
    },
  }
})